<!--

-->
<template>
    <div class="GFileListPreview">
        <el-button v-for="item in list" :key="item.id" class="preview" type="primary" link @click="show(item)">
            <div class="name">
                {{getFileName(item.path)}}
            </div>
        </el-button>
    </div>
</template>

<script>
export default {
    name: "GImageListPreview",
    emits: ["update:modelValue"],
    props: {
        modelValue: {
            type: String,
            default: () => ""
        },
        src: {
            type: String,
            default: () => ""
        }
    },
    data() {
        return {
            list: []
        }
    },
    watch: {
        modelValue() {
            this.setListValue()
        },
        src() {
            this.setListValue()
        },
    },
    mounted() {
        this.setListValue();
    },
    methods: {
        getFileName(path) {
            if (path.indexOf("：") > -1) return path.split("：").at(-1)
            if (path.indexOf("/") > -1) return path.split("/").at(-1)
            return path
        },
        setListValue() {
            if (this.modelValue) {
                const list = this.modelValue.split(";").filter(item => item).map(item => {
                    return {
                        path: item,
                        id: item
                    }
                })
                this.list = list;
            } else if (this.src) {
                const list = this.src.split(";").filter(item => item).map(item => {
                    return {
                        path: item,
                        id: item
                    }
                })
                this.list = list;
            }
        },
        imageFullPath(val) {
            const host = window.$config.baseUrl;
            return host + "/" + val;
        },

        show(item) {
            this.showBig(this.imageFullPath(item.path));
        },
        showBig(fullPath) {
            const width = 500;
            const height = 600;
            const left = (document.documentElement.clientWidth - width) / 2;
            window.open(fullPath, "", `width=${width}, height=${height}, left=${height}, top=160`)
        }
    }
}
</script>

<style lang="scss" scoped>
.GFileListPreview {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    flex-wrap: wrap;
    height: auto;
    .preview {
        .name {
            max-width: 300px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    }
}
</style>
